package org.lds.simple.disruptor.producer;

import lombok.Data;
import org.lds.simple.disruptor.RingBuffer;
import org.lds.simple.disruptor.event.Event;
import org.lds.simple.disruptor.producer.Producer;

@Data
public class AbstractProducer<T extends Event, E> implements Producer<T, E> {
    private RingBuffer<T> ringBuffer;

    public AbstractProducer(RingBuffer<T> ringBuffer) {
        this.ringBuffer = ringBuffer;
    }

    @Override
    public void send(E data) {
        long producerSequence = ringBuffer.next();
        if (producerSequence == Long.MIN_VALUE) return;

        try {
            T event = ringBuffer.getEvent(producerSequence);
            convert(event, data);
        } finally {
            ringBuffer.publish(producerSequence);
        }
    }

    @Override
    public void convert(T t, E data) {

    }

}
